import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '电推刀-恶搞理发剃头',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  AudioPlayer fixedPlayer = AudioPlayer();
  AudioCache player;
  // 音频是否正在播放
  bool complete = false;

  // 开始播放
  play() {
    player.loop("preview.mp3");
  }

  // 暂停播放
  void pause() async {
    fixedPlayer.pause();
  }

  @override
  void initState() {
    player = AudioCache(fixedPlayer: fixedPlayer);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/bbg.png"),
            repeat: ImageRepeat.repeat,
            fit: BoxFit.fill,
          ),
        ),
        child: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("assets/bg.png"),
              repeat: ImageRepeat.repeat,
              fit: BoxFit.fitHeight,
            ),
          ),
          child: Stack(
            alignment: AlignmentDirectional.center,
            children: [
              Positioned(
                child: InkWell(
                  onTap: () {
                    complete ? pause() : play();
                    setState(() => {complete = !complete});
                  },
                  child: Container(
                    margin: EdgeInsets.only(top: 70),
                    width: MediaQuery.of(context).size.width / 7,
                    child: complete
                        ? Image.asset('assets/on.png')
                        : Image.asset('assets/off.png'),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
